package com.kingsoft.dc.khaos.extender.meta.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue;

/**
 * Created by jing on 19/6/25.
 */
public class TableAuthParams implements Serializable {
    private static final long serialVersionUID = 1L;

    private String env;
    private int jobProjectId;
    private String jobProjectName;
    private int dsId;
    private String dsName;
    private List<AuthTable> authTables;

    public TableAuthParams() {

    }

    public TableAuthParams buildAuthParams(String env, int jobProjectId, int dsId, List<AuthTable> authTables) {
        this.env = env.toUpperCase();
        this.jobProjectId = jobProjectId;
        this.dsId = dsId;
        this.authTables = authTables;
        return this;
    }

    public TableAuthParams buildAuthParams(String env, int jobProjectId, String jobProjectName, int dsId, String dsName, List<AuthTable> authTables) {
        this.env = env.toUpperCase();
        this.jobProjectId = jobProjectId;
        //this.jobProjectName = jobProjectName;
        this.dsId = dsId;
        this.dsName = dsName;
        this.authTables = authTables;
        return this;
    }


    /**[{"dbName":"%s","tableName":"%s","privSet":[%s]}]
     * @return
     */
    public String getAuthParams() {
        Map<String, Object> var1 = new HashMap<>();
        Map<String, Object> projectParam = new HashMap<>();
        if (jobProjectId > 0) {
            projectParam.put("projectId", String.valueOf(jobProjectId));
        }
        //projectParam.put("projectName", jobProjectName);
        var1.put("projectParam", projectParam);
        var1.put("metadataPrivsList", getMetaList(authTables));
        var1.put("env", env.toUpperCase());
        if (dsId > 0) {
            var1.put("dataSourceId", String.valueOf(dsId));
        }
        var1.put("dataSourceName", dsName);
        return JSON.toJSONString(var1, WriteMapNullValue);
//        Map<String, Object> projectParam = new HashMap<>();
//        projectParam.put("projectId" , jobProjectId);
//        projectParam.put("projectName", jobProjectName);
//        return String.format("{\"projectParam\":%s,\"metadataPrivsList\":%s,\"env\":\"%s\",\"dataSourceId\":\"%d\"}",
//                JSON.toJSONString(projectParam, SerializerFeature.WriteMapNullValue), getJsonString(authTables), env, dsId);
    }

    public List<Map<String, Object>> getMetaList(List<AuthTable> authTables) {
        List<Map<String, Object>> metaMapList  = new ArrayList<>();
        for (AuthTable authTable : authTables) {
            Map<String, Object> metaMap = new HashMap();
            metaMap.put("dbName", authTable.getDbName());
            metaMap.put("tableName", authTable.getTblName());
            List<String> newPrivs = new ArrayList<>();
            String[] privSet = authTable.getPrivSet();
            for (String priv: privSet) {
                newPrivs.add(priv.toUpperCase());
            }
            metaMap.put("privSet", newPrivs);
            metaMapList.add(metaMap);
        }
        return metaMapList;
    }

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }

    public int getJobProjectId() {
        return jobProjectId;
    }

    public void setJobProjectId(int jobProjectId) {
        this.jobProjectId = jobProjectId;
    }

    public int getDsId() {
        return dsId;
    }

    public void setDsId(int dsId) {
        this.dsId = dsId;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public List<AuthTable> getAuthTables() {
        return authTables;
    }

    public void setAuthTables(List<AuthTable> authTables) {
        this.authTables = authTables;
    }

    public String getJobProjectName() {
        return jobProjectName;
    }

    public void setJobProjectName(String jobProjectName) {
        this.jobProjectName = jobProjectName;
    }

    public String getDsName() {
        return dsName;
    }

    public void setDsName(String dsName) {
        this.dsName = dsName;
    }
}
